import reqAddress from '../req-set.js'
let state = {
	basicsGoodesList: [],
	page: 1,
}
let mutations = {
	setBasicsGoodesList(state, v) {
		state.basicsGoodesList = v
	},
	setGoodesListLoadMore(state, v) {
		state.basicsGoodesList = state.basicsGoodesList.concat(v)
	},
	setPage(state, v) {
		state.page = v
	}
}

let actions = {
	//拿到基础数据
	reqBasicsGoodesList(store, param) {
		return new Promise((resolve, reject) => {
			oneHandleEvent({
				store,
				param,
				url: `/command?`
			}).then(data => {
				resolve(data)
			})

		})

	},

	//获取商品类型列表
	reqHomeGoodesTypeList(store, param) {
		return new Promise((resolve, reject) => {
			oneHandleEvent({
				store,
				param,
				url: `/hot?material_id=${param.idx}`
			}).then(data => {
				resolve(data)
			})
		})
	},

	//加载更多处理
	reqExemptionLoadMore(store, param) {
		return new Promise((resolve, reject) => {
			let page = store.state.page
			let data_Temp = []
			async function reqBasic() {
				while (data_Temp.length < 20) {
					let data = await handleReqBasice(`${reqAddress}${param.url}&page=${page}`)
					page = page + 1
					data.forEach(el => {
						if (el.coupon_start_fee && el.coupon_amount) {
							let minus = el.coupon_start_fee * 1 - el.coupon_amount * 1
							//判断优惠价格
							if (minus <= param.preferential) {
								data_Temp.push(el)
							}
						}
					})
				}
				if (data_Temp.length >= 19) {
					let mutaion = 'setGoodesListLoadMore'
					store.commit("setPage", page)
					goodesDataHandle(store, data_Temp, param.active, mutaion)
					resolve('成功')
				} else {
					reject("失败")
				}
			}
			reqBasic()
		})
	}

}

function oneHandleEvent({
	store,
	param,
	url
}) {
	store.commit("setBasicsGoodesList", '')
	return new Promise((resolve, reject) => {
		let page = 1
		let data_Temp = []
		async function reqBasic() {
			while (data_Temp.length < 20) {
				let data = await handleReqBasice(`${reqAddress}${url}&page=${page}`)
				page = page + 1
				data.forEach(el => {
					if (el.coupon_start_fee && el.coupon_amount) {
						let minus = el.coupon_start_fee * 1 - el.coupon_amount * 1
						//判断优惠价格
						if (minus <= param.preferential) {
							data_Temp.push(el)
						}
					}
				})
				console.log('函数内')
			}
			if (data_Temp.length >= 19) {
				let mutaion = 'setBasicsGoodesList'
				//设置page页,告知加载更多的时从哪里开始
				store.commit("setPage", page)

				goodesDataHandle(store, data_Temp, param.active, mutaion)
				resolve('成功')
			} else {
				reject("失败")
			}
		}
		reqBasic()
	})
}
//摘选9.9包邮
function handleReqBasice(url) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			method: 'GET',
			success: (resp) => {
				let dataTemp = resp.data.result_list.map_data
				resolve(dataTemp)

			}
		})
	})
}
//商品列表数据处理函数
function goodesDataHandle(store, resp, param, mutaion) {
	let temp = resp
	//渲染时使用 ---------- --- --- -  -
	for (let i = 0; i < temp.length; i++) {
		temp[i].src = 'https://img.alicdn.com/bao/uploaded/i2/915497723/O1CN01Cqq4tK26vBNKEzU6M_!!0-item_pic.jpg'
		temp[i].show = true
		temp[i].loaded = false
	}
	switch (param) {
		case 0:
			store.commit(mutaion, temp)
			break
		case 1:
			let caseOne = temp.sort((n, m) => {
				if (n.coupon_amount * 1 > m.coupon_amount * 1) {
					return -1
				} else if (n.coupon_amount * 1 < m.coupon_amount * 1) {
					return 1
				} else {
					return 0
				}
			})
			store.commit(mutaion, caseOne)
			break
		case 2:
			let caseTwo = temp.sort((n, m) => {
				if (n.volume * 1 > m.volume * 1) {
					return -1
				} else if (n.volume * 1 < m.volume * 1) {
					return 1
				} else {
					return 0
				}
			})
			store.commit(mutaion, caseTwo)
			break
		case 3:
			let caseThree = temp.sort((n, m) => {
				if (n.coupon_start_fee * 1 > m.coupon_start_fee * 1) {
					return 1
				} else if (n.coupon_start_fee * 1 < m.coupon_start_fee * 1) {
					return -1
				} else {
					return 0
				}
			})
			store.commit(mutaion, caseThree)
			break
		default:
			break;
	}
}
let exemption_idx = {
	state,
	mutations,
	actions
}
export default exemption_idx
